//
//  HMSinaStatusesCell.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/18.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit
import SDWebImage
import YYText

//图片视图之间的间距
private let pictureJianju : CGFloat = 2
private let maxWidth = UIScreen.main.bounds.width - 2 * 8
private let itemWidth = (maxWidth - 2 * pictureJianju ) / 3

class HMSinaStatusesCell: UITableViewCell {
    
    //用户头像
    @IBOutlet weak var iconImage: UIImageView!
    //用户名称
    @IBOutlet weak var nameLabel: UILabel!
    //用户等级
    @IBOutlet weak var levelImage: UIImageView!
    //微博事件
    @IBOutlet weak var timeLabel: UILabel!
    //微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    //微博正文
    @IBOutlet weak var contentLabel: YYLabel!
    //微博认证类型
    @IBOutlet weak var memberShipImage: UIImageView!

    //配图视图
    @IBOutlet weak var picturesView: HMSinaPicturesCollectionView!
    //图片视图的高度
    @IBOutlet weak var pictureHeight: NSLayoutConstraint!
    //图片视图的宽度
    @IBOutlet weak var pictureWidth: NSLayoutConstraint!
    //关联配图视图的布局对象
    @IBOutlet weak var pictureViewFollowLayout: UICollectionViewFlowLayout!
    //底部工具条
    @IBOutlet weak var toolsBar: UIView!
    //topview顶部约束
    @IBOutlet weak var pictureViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var zhuanFaLabel: UILabel!
    
    
    
    //cell需要绑定数据
    var statusesModel : HMSinaStatusesCellViewModel?{
    
        didSet{
        
            //设置子控件的数据
            iconImage.sd_setImage(with: statusesModel?.iconURL)
            nameLabel.text = statusesModel?.statusesCell?.user?.name
            timeLabel.text = statusesModel?.timeText
            sourceLabel.text = statusesModel?.sourceText
            contentLabel.attributedText = statusesModel?.originalString
            memberShipImage.image = statusesModel?.userTypeImage
            levelImage.image = statusesModel?.levelImage
            
            let count = statusesModel?.imageURL?.count ?? 0
            if count == 0 {
                
                pictureViewTopConstraint.constant = 0
                
            }
            
            pictureViewTopConstraint.constant = 8
            
            let picturSize = calculatePicturesViewSize(count: count)
            
            //更新约束对象的约束值
            pictureWidth.constant = picturSize.mangPictureSize.width
            pictureHeight.constant = picturSize.mangPictureSize.height
            //还需要更新itemSize
            pictureViewFollowLayout.itemSize = picturSize.singlePictureSize
            
            //给视图设置数据源、转发微博的视图显示
            //转发微博和原创微博都是共用了一样的视图
            picturesView.sinaPictures = statusesModel?.imageURL
            
            //解决快速上拉下拉图片不读取的方法(原因是自动更新不会立即生效)
            picturesView.layoutIfNeeded()
            //转发视图的文本显示
            zhuanFaLabel?.attributedText = statusesModel?.zhuanFaString
            
            //实现点击高亮的闭包
            contentLabel.highlightTapAction = {(containerView , text , range , rect) -> () in
                
                //containerView就是被点击的视图,
                //text即使被点击的属性文本,range
                //range:点击的特殊文本的范范围
                //rect:点击的特殊文本的坐标
                let sub = (text.string as NSString).substring(with: range)
                
                //跳转页面 响应者链条实现跳转
                //当用户点击的是url的时候，就要跳转到对应的网页
                //判断如果是http开头的，就是一个网页
                if sub.hasPrefix("http") {
                    
                    //实例化控制器
                    let webController = HMSinaWebViewController()
                    
                    //push控制器 在cell的响应者链条中获取导航视图控制器
                    webController.urlString = sub
                    
                    self.findNavigationController()?.pushViewController(webController, animated: true)
                    
                }
                
            }
        }
    
    }
    
        override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置背景颜色
        //        self.contentView.backgroundColor = randomColor()
        
        //设置自动换行
        self.contentLabel.numberOfLines = 0
        
        //设置最大宽度
        self.contentLabel.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 8
        
        //设置自动换行
        self.zhuanFaLabel?.numberOfLines = 0
        
        //设置最大宽度
        self.zhuanFaLabel?.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 8
        
        //设置流水布局
        pictureViewFollowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        //设置行和列的最小间距
        pictureViewFollowLayout.minimumLineSpacing = pictureJianju
        pictureViewFollowLayout.minimumInteritemSpacing = pictureJianju
        //设置选中样式的取消
        selectionStyle = .none
        
        }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
    }
    
    //根据图片的张数计算图片视图的大小，更新图片视图的宽和高的约束对象的值
    //元组类型：可以使用多个类型合成一个类型
    private func calculatePicturesViewSize(count : Int) -> (mangPictureSize : CGSize , singlePictureSize : CGSize) {
        
        if count == 0 {
            return (CGSize.zero , CGSize.zero)
        }
        
        if count == 1 {
            
            //计算图片的大小，获取图片.必须确保在这里一定能获取到图片
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: statusesModel?.imageURL?.last?.wap360 ?? ""){
            
                let imageSize = image.size
                //不能在if中单独设置单张图片的imageSize
//                pictureViewFollowLayout.itemSize = imageSize
            
                return (imageSize , imageSize)
            }
          return (CGSize(width: itemWidth, height: itemWidth) , CGSize(width: itemWidth, height: itemWidth))
            
        }
        
        //判断四张图片
        if count == 4 {
            
            let width = 2 * itemWidth + 8
            return (CGSize(width: width, height: width) , CGSize(width: itemWidth, height: itemWidth))
            
        }
        
        //其他的多张图片需要确定行数
        /*
         1 2 3
         4 5 6
         7 8 9
         */
        let row = CGFloat((count - 1) / 3 + 1)
        let height = (row * itemWidth) + (row - 1) * pictureJianju
        
        return (CGSize(width: maxWidth, height: height) , CGSize(width: itemWidth, height: itemWidth))
        
    }
    
}
